#encoding:UTF-8
import urllib2
import re
import json
import mylib
import pythonlibSB


def get_hot_link():
    
    try:
        results = []
        hot_10_link = []
        
        content = mylib.get_content('http://dantri.com.vn/')
        hot_content = pythonlibSB.s_string(content,'class="fl wid620"','div')
        hot_10_content = pythonlibSB.s_string(content,'class="fl wid335 noibattrangchu"','div')
        reg = re.compile('(h1><a)(.*?)(href=")(.*?)(")')
        if reg.search(hot_10_content):
            hot10url = reg.search(hot_10_content).group(4)
            real10link = "http://dantri.com.vn"+hot10url if hot10url.find('http://')==-1 else hot10url
            hot_10_link.append(real10link)
        results.append(hot_10_link)
        
        hot_9_link = []
        hot_9_content = pythonlibSB.s_string(hot_content,'class="box3 clearfix mb2"','div')
        for i in re.finditer('(h2><a)(.*?)(href=")(.*?)(")',hot_9_content):
            hot9url = ('http://dantri.com.vn'+i.group(4)) \
                    if i.group(4).find('http://')==-1 else i.group(4)
            hot_9_link.append(hot9url)
        results.append(hot_9_link)
        
        hot_8_link = []
        hot_8_content = pythonlibSB.s_string(content,'class="fr wid268 box2"','div')
        for j in re.finditer('(<a)(.*?)(href=")(.*?)(")',hot_8_content):
            hot8url = j.group(4) if j.group(4).find('http://')!=-1 else ('http://dantri.com.vn'+j.group(4))
            hot_8_link.append(hot8url)
        hot_8_link.remove('http://dantri.com.vn/c728/sukien.htm')
        results.append(hot_8_link)
        
        hot_7_link = []
          
        results.append(hot_7_link)
        
        
        return results

    except :
        return []
if __name__ == "__main__":
    hot10 = get_hot_link()
    print hot10
    hot8 = hot10[3]
    print hot8
    print len(hot8)
        